package cn.g31010.sip.component
{
	import flash.events.AsyncErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.net.SharedObject;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	public class Communication
	{
		public var sharedObject:SharedObject;
		public var address:String;
		public var nc:NetConnection;
		 
		public function Communication(address:String)
		{
			this.address=address;
		}
	
		public function connect(event:CloseEvent=null):void{
			if(isConnected()){
				close();
			}
			clear();
			if(event==null||event.detail == Alert.OK){
				NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
				nc=new NetConnection();
				nc.client=new Client();
				nc.addEventListener (NetStatusEvent.NET_STATUS,checkConnect);
				nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncerr);
				nc.connect(address);
				
			}
		}
		public function isConnected():Boolean{
			if(nc==null)
			return false;
			return nc.connected;
		}
		
		private var closed:Boolean=false;
		public function close():void{
			closed=true;
			if(ns!=null){
				ns.attachAudio(null);
				ns.attachCamera(null);
				ns.close();
			}
			if(nc!=null){
				nc.close();
			}
			clear();
		}
		
		public function getName():void
		{
			trace( "getName: " + txtUser.text );
			sharedObject.setProperty("key",txtUser.text);
		}

		public function newName(str:String):void
		{
			arr.push(str);
			sharedObject.setProperty("arr",arr);
			con = new ArrayCollection(arr);
		}

		private function sharedObjectSyncHandler( event:SyncEvent ):void
		{
			trace( "sharedObjectSyncHandler:code: " + event.changeList );
			arr.push(event.target.data.key);
			con = new ArrayCollection(arr);
		}

		private function netStatusHandler( event:NetStatusEvent ):void
		{
			trace( "netStatusHandler:code: " + event.info.code );
		}
		
		private function asyncErrorHandler( event:AsyncErrorEvent ):void
		{
			trace( "asyncErrorHandler:code: " + event.error );
		}

		internal function asyncerr(e:AsyncErrorEvent):void{
            Alert.show(e.error.message);
        }
        
		internal function checkConnect (e:NetStatusEvent):void{
			var connected:Boolean=(e.info.code=="NetConnection.Connect.Success");
			closed=false;
			if (connected){
				sharedObject=SharedObject.getRemote("",nc.uri,false,false);
				sharedObject.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
				sharedObject.addEventListener( AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler );
				sharedObject.addEventListener( SyncEvent.SYNC, sharedObjectSyncHandler );
				
				sharedObject.client = this;
				sharedObject.connect( nc );
				sharedObject.send("");
			}else if(e.info.code=="NetConnection.Connect.Closed"){

			}else{
				if(autoConnect){
					connect();
				}else{
					Alert.show(e.info.code+"\n连接失败，是否重新连接？","提示：",Alert.OK|Alert.NO,null,connect);
				}
				
			}
		}
	}
}